﻿using IceZero.DataStore.Models;
using IceZero.DataStore.Models.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceZero.DataStore.InitDB
{
    public partial class DbInitializer
    {
        /// <summary>
        /// 初始化自增模式数据
        /// </summary>
        public void InitIncrementModeModel()
        {
            var dataStore = new DataStoreBase<IncrementModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new IncrementModeModel()
                {
                    Remark = $"自增id:{i}"
                });
            }
        }

        /// <summary>
        ///     
        /// </summary>
        public void InitAssignedModeModel()
        {
            var dataStore = new DataStoreBase<AssignedModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 5; i++)
            {
                dataStore.Save(new AssignedModeModel()
                {
                    Id = "ID" + i,
                    Remark = $"自己分配id:{i}"
                });
                dataStore.Save(new AssignedModeModel()
                {
                    Id = DateTime.Now.ToString() + i,
                    Remark = $"自己分配id:{i}"
                });
                dataStore.Save(new AssignedModeModel()
                {
                    Id = "EX00" + i,
                    Remark = $"自己分配id:{i}"
                });
                dataStore.Save(new AssignedModeModel()
                {
                    Id = i.ToString(),
                    Remark = null
                });
            }
            dataStore.Save(new AssignedModeModel()
            {
                Id = "",
                Remark = "empty id"
            });
            /*
            dataStore.Save(new AssignedModeModel()
            {
                Id =null,
                Remark = "null id"
            });
            */
            dataStore.Save(new AssignedModeModel()
            {
                Id = "null",
                Remark = "null string id"
            });
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitGuidCombModeModel()
        {
            var dataStore = new DataStoreBase<GuidCombModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new GuidCombModeModel()
                {
                    Remark = $"使用Jimmy Nilsson的算法（请参阅 http://www.informit.com/articles/article.asp?p=25862 ）生成一个新的System.Guid标示符:{i}"
                });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitGuidModeModel()
        {
            var dataStore = new DataStoreBase<GuidModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new GuidModeModel()
                {
                    Remark = $"使用新的System.Guid实例作为标示符:{i}"
                });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitUuidStringModeModel()
        {
            var dataStore = new DataStoreBase<UuidStringModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new UuidStringModeModel()
                {
                    Remark = $"用一个新的System.Guid实例的byte[]转化为字符串作为标示符:{i}"
                });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitUuidHexModeModel()
        {
            var dataStore = new DataStoreBase<UuidHexModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new UuidHexModeModel()
                {
                    Remark = $"用一个System.Guid的ToString()方法法生成字符串类型的标识符， 字符串的长度由format参数定义:{i}"
                });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitNativeModeModel()
        {
            var dataStore = new DataStoreBase<NativeModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new NativeModeModel()
                {
                    Remark = $"根据底层数据库的能力选择identity, sequence 或者hilo中的一个:{i}"
                });
            }
        }

        /// <summary>
        /// 挑数据库,使用自增序列代替
        /// </summary>
        public void InitSequenceModeModel()
        {
            var dataStore = new DataStoreBase<SequenceModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new SequenceModeModel()
                {
                    Remark = $"sequence ,在DB2,PostgreSQL, Oracle, SAP DB, McKoi中使用序列（sequence)" +
                    $"，而在Interbase中使用生成器(generator)。返回的标识符是int类型的:{i}"
                });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitHiloModeModel()
        {
            var dataStore = new DataStoreBase<HiloModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new HiloModeModel()
                {
                    Remark = $"使用一个高/低位算法来高效的生成int类型的标识符。给定一个表和字段" +
                    $"（默认分别是是hibernate_unique_key 和next_hi）作为高位值得来源。 高/低位算法" +
                    $"生成的标识符只在一个特定的数据库中是唯一的。在用户自行提供的连接中，不要使用" +
                    $"这种生成器。你可以使用where参数来指定表里面用了多少列数据， You can use the" +
                    $"\"where\" parameter to specify the row to use in a table.This is useful " +
                    $"if you want to use a single tabel for your identifiers, with different rows for each table.:{ i}"
                });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitIdentityModeModel()
        {
            var dataStore = new DataStoreBase<IdentityModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new IdentityModeModel()
                {
                    Remark = $"对DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持。数据库返回的主键值 返回的标识符是int类型的。:{i}"
                });
            }
        }

        /// <summary>
        /// 挑数据库,使用自增序列代替
        /// </summary>
        public void InitSeqhiloModeModel()
        {
            var dataStore = new DataStoreBase<SeqhiloModeModel>();
            if (dataStore.Count() > 0)
            {
                return;
            }
            for (int i = 1; i <= 15; i++)
            {
                dataStore.Save(new SeqhiloModeModel()
                {
                    Remark = $"使用一个高/低位算法来高效的生成int类型的标识符，给定一个数据库序列（sequence)的名字:{i}"
                });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitForeignModeModel()
        {
            var dataStore = new DataStoreBase<ForeignModeModel>();
            var dataStore2 = new DataStoreBase<ForeignModeThirdModel>();
            if (dataStore.Count() == 0)
            {
                for (int i = 1; i <= 15; i++)
                {
                    dataStore.Save(new ForeignModeModel()
                    {
                        Remark = $"使用另外一个相关联的对象的标识符。通常和<one-to-one>联合起来使用:{i}"
                    });
                }
            }
            var datas = dataStore.QueryAll();
            if (dataStore2.Count() == 0)
            {
                for (int i = 0; i < 15; i++)
                {
                    var d = datas[i];
                    dataStore2.Save(new ForeignModeThirdModel()
                    {
                        Id = d.Id,
                        Remark = $"one to one 关联:{i}"
                    });
                }
                //插入会报错，因为主键不在ForeignModeModel 表
                //dataStore2.Save(new ForeignModeThirdModel()
                //{
                //    Id = "001",
                //    Remark = $"one to one 关联"
                //});
                //插入会报错
                //dataStore2.Save(new ForeignModeThirdModel()
                //{
                //    Id = "002",
                //    Remark = $"one to one 关联"
                //});
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void InitPrimaryKeyModeModels()
        {
            InitIncrementModeModel();
            InitAssignedModeModel();
            InitGuidCombModeModel();
            InitGuidModeModel();
            InitUuidStringModeModel();
            InitUuidHexModeModel();
            InitNativeModeModel();
            InitSequenceModeModel();
            InitHiloModeModel();
            InitIdentityModeModel();
            InitSeqhiloModeModel();
            InitForeignModeModel();
        }
    }
}
